<template>
    <div>
  <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
    <el-form-item label="名称" prop="name">
      <el-input v-model="ruleForm.name"></el-input>
    </el-form-item>
  
    <el-form-item label="图片上传" prop="pic">
      <el-upload
    class="avatar-uploader"
    action="https://jsonplaceholder.typicode.com/posts/"
    :show-file-list="false"
    :on-success="handleAvatarSuccess"
    :before-upload="beforeAvatarUpload">
    <img v-if="imageUrl" :src="imageUrl" class="avatar">
    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
  </el-upload>
    </el-form-item>
  
    <el-form-item label="选择标签" prop="tags">
      <el-checkbox-group v-model="ruleForm.tags">
        <el-checkbox :label="i.name" name="type" v-for="(i,index) in tags" :key="index"></el-checkbox>
  
      </el-checkbox-group>
    </el-form-item>
  
    <el-form-item label="选择分类" prop="ctype">
      <el-checkbox-group v-model="ruleForm.ctype">
        <el-checkbox label="美食/餐厅线上活动" name="type"></el-checkbox>
  
      </el-checkbox-group>
    </el-form-item>
  
  
    <el-form-item label="选择类别" prop="type">
      <el-radio-group v-model="ruleForm.type">
        <el-radio label="免费"></el-radio>
  
      </el-radio-group>
    </el-form-item>
     <el-form-item label="简介" prop="desc">
      <el-input type="textarea" v-model="ruleForm.desc"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
      <el-button @click="resetForm('ruleForm')">重置</el-button>
    </el-form-item>
  </el-form>
    </div>
  </template>
  
  <script>
  export default {
      data() {
        return {
          tags:[],
          types:[],
          clist:[],
          imageUrl: '',
          ruleForm: {
            name: '',
            tags:[],
            ctype:[],
            type:'',
            desc: ''
          },
          rules: {
            name: [
              { required: true, message: '请输入活动名称', trigger: 'blur' },
              { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
            ],
            ctype: [
              { type: 'array', required: true, message: '请至少选择一个分类', trigger: 'change' }
            ],
            desc: [
              { required: true, message: '请填写活动形式', trigger: 'blur' }
            ]
          }
        };
      },
      methods: {
          getTags(){
              this.axios.get("user/getTags").then(res=>{
                  this.tags = res.data.tags
                  this.types = res.data.types
                  this.clist = res.data.clist
              })
          },
        submitForm(formName) {
          this.$refs[formName].validate((valid) => {
            if (valid) {
              alert('submit!');
            } else {
              console.log('error submit!!');
              return false;
            }
          });
        },
        resetForm(formName) {
          this.$refs[formName].resetFields();
        },
        handleAvatarSuccess(res, file) {
          this.imageUrl = URL.createObjectURL(file.raw);
        },
        beforeAvatarUpload(file) {
          const isJPG = file.type === 'image/jpeg';
          const isLt2M = file.size / 1024 / 1024 < 2;
  
          if (!isJPG) {
            this.$message.error('上传头像图片只能是 JPG 格式!');
          }
          if (!isLt2M) {
            this.$message.error('上传头像图片大小不能超过 2MB!');
          }
          return isJPG && isLt2M;
          }
      },
      mounted(){
          this.getTags()
      }
  }
  </script>
  
  <style>
  
  </style>